/**
* The Game System object-
* Initializes the game & library, tells the assets
* where they can load their files from (based on config file)
*
* @date 2011-04-08
* @author Rachel J. Morris
*
* http://www.moosader.com/
* http://code.google.com/p/unicorn-princess/
* GNU GPL v3 license
*/

// I'm updating Unicorn Princess! I've learned a lot since 2009,
// despite working in web development for the past year.
// Also I've gotten used to C#, so I'm getting rid of .cpp files >_>

// I'm going to refactor this further, but for now I'm getting rid of the singletons.

#ifndef _GameSys
#define _GameSys

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

#include <iostream>
#include <fstream>

#include "_Global.h" // going to rename to hpp once it's restructured
#include "MooseArray.hpp"

class GameSys
{
    private:
        int _scrWidth, _scrHeight, _bpp;
        bool _fullscreen;
        sf::RenderWindow _appWindow;
        std::string _gameTitle;
        MooseArray<sf::Sprite> _renderList;

        void LoadConfig()
        {
            std::ifstream infile( CONFIG_FILE.c_str() );
            if ( infile == NULL )
            {
                CreateConfig();
            }
            else
            {
                std::string key;
                std::string value;
                while ( infile >> key )
                {
                    infile>>value;
                    if ( key == "screenWidth" )
                    {
                        _scrWidth = StringToInt( value );
                    }
                    else if ( key == "screenHeight" )
                    {
                        _scrHeight = StringToInt( value );
                    }
                    else if ( key == "fullscreen" )
                    {
                        _fullscreen = StringToInt( value );
                    }
                    else if ( key == "gameTitle" )
                    {
                        _gameTitle = value;
                    }
                }
            }

            infile.close();
        }

        void CreateConfig()
        {
            _gameTitle = "game";
            _scrWidth = 1024;
            _scrHeight = 768;
            _fullscreen = false;

            std::ofstream outfile( "config.txt" );
                outfile<<"gameTitle "<<_gameTitle<<std::endl;
                outfile<<"screenWidth "<<_scrWidth<<std::endl;
                outfile<<"screenHeight "<<_scrHeight<<std::endl;
                outfile<<"fullscreen "<<_fullscreen<<std::endl;
                outfile<<"lvl_dir data/lvl/"<<std::endl;
                outfile<<"gfx_dir data/gfx/"<<std::endl;
                outfile<<"snd_dir data/snd/"<<std::endl;
                outfile<<"font_dir data/font/"<<std::endl;
            outfile.close();
        }
    public:
        GameSys() { Init(); }
        ~GameSys() { Close(); }

        void Init()
        {
            LoadConfig();
            _bpp = 32;

            _appWindow.Create( sf::VideoMode( _scrWidth, _scrHeight, _bpp ), _gameTitle );
            _appWindow.SetFramerateLimit( 60 );
            _appWindow.Clear();
        }

        void Close()
        {
            _appWindow.Close();
        }

        /* WINDOW FUNCTIONALITY */

        sf::RenderWindow& AppWindow()
        {
            return _appWindow;
        }

        void ToggleFullscreen()
        {
            _fullscreen = !_fullscreen;
            if ( _fullscreen )
            {
                _appWindow.Create( sf::VideoMode( _scrWidth, _scrHeight, _bpp ), _gameTitle, sf::Style::Fullscreen );
            }
            else
            {
                _appWindow.Create( sf::VideoMode( _scrWidth, _scrHeight, _bpp ), _gameTitle );
            }
        }

        /* DRAWING FUNCTIONALITY */

        void AddToRenderList( sf::Sprite& sprite )
        {
            _renderList.Push( sprite );
        }

        void DrawScreen()
        {
            _appWindow.Clear();
            for ( int i=0; i<_renderList.Size(); i++ )
            {
                _appWindow.Draw( _renderList[i] );
            }
            _appWindow.Display();
            _renderList.Clear();
        }
};

#endif




